package com.huike.popupwindow;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_main_alert;
    private PopupWindow popupWindow;
    private View view;
    private int height;
    private int width;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        view = View.inflate(this, R.layout.popu_alert, null);

        view.measure(0,0);
        height = view.getMeasuredHeight();
        width = view.getMeasuredWidth();
        popupWindow = new PopupWindow(view,150,200);
        popupWindow.setOutsideTouchable(true);//设置外部点击可以消失
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

    }

    private void initView() {
        btn_main_alert = (Button) findViewById(R.id.btn_main_alert);
        btn_main_alert.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_alert:
                popupWindow.showAsDropDown(btn_main_alert,0,-height-60);
                break;
        }
    }
}
